package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysf.library_boot.entity.Admin;
import com.zysf.library_boot.entity.ReaderInfo;
import com.zysf.library_boot.service.AdminService;
import com.zysf.library_boot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("/getAdmin")
    public Result listAdmin(@RequestParam(required = false) String username,
                            @RequestParam(required = false) Integer admintype,
                            @RequestParam int currentPage,
                            @RequestParam int pageSize){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(username)){
            queryWrapper.like("username",username);
        }
        if (admintype!=null){
            queryWrapper.eq("admintype",admintype);
        }
        Page<Admin> page = new Page<>(currentPage,pageSize);
        IPage<Admin> adminIPage = adminService.page(page, queryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("records",adminIPage.getRecords());
        result.put("total",adminIPage.getTotal());
        return Result.ok(result);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean e = adminService.removeById(id);
        return Result.ok(e);
    }

    @RequestMapping("/batchDeleteAdmin")
    public Result batchDeleteAdmin(@RequestBody List<Integer> ids){
        boolean f = adminService.removeBatchByIds(ids);
        return Result.ok(f);
    }

    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody Admin admin) {
        boolean saved = adminService.save(admin);
        if (saved) {
            return Result.ok("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    @RequestMapping("/checkUsername")
    public Result checkUsername(@RequestParam String username,@RequestParam(required = false) Long id) {
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        qw.eq("username", username);
        if (id != null) {
            qw.ne("id", id);
        }
        Admin admin = adminService.getOne(qw);
        return Result.ok(admin);
    }

    @PostMapping("/updateAdmin")
    public Result updateAdmin(@RequestBody Admin admin) {
        boolean updated = adminService.updateById(admin);
        if (updated) {
            return Result.ok("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

    @RequestMapping("/AdminCount")
    public Result AdminCount(){
        QueryWrapper<Admin> qwAdmin = new QueryWrapper<>();
        qwAdmin.eq("role",1);
        int admincount = (int) adminService.count(qwAdmin);
        Map<String,Object> result = new HashMap<>();
        result.put("admincount",admincount);
        return Result.ok(result);
    }
}
